<template>
  <div class="main-container">
    <!-- 底层地图 - 始终保持显示 -->
    <div class="map-layer" :class="{ 'map-mode': isMapMode }">
      <MapContainer ref="mapContainer" @mapReady="onMapReady" />
    </div>

    <!-- 地图操作面板 -->
    <MapOperationPanel :mode="activeMode" :is-visible="isMapMode" @close="exitMapMode" />

    <!-- 上层内容区域 - 使用路由 -->
    <div class="content-layer" :class="{ 'content-visible': !isMapMode }">
      <div class="content-area">
        <router-view />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import MapContainer from './MapContainer.vue'
import MapOperationPanel from './MapOperationPanel.vue'
import { mapController } from './MapController'

// Props
const props = defineProps({
  isLoggedIn: {
    type: Boolean,
    default: false
  }
})

// Emits
const emit = defineEmits(['menuChange'])

// 路由
const route = useRoute()
const router = useRouter()

// 状态
const mapContainer = ref()
const isMapMode = ref(false) // 地图模式状态

const activeMode = computed(() => {
  return isMapMode.value ? (route.name as string) : ''
})

// 初始化函数
const initialize = () => {
  // 默认路由
  if (route.path === '/') {
    router.push('/yukoume')
  }
}

// 监听路由变化
watch(() => route.path, (newPath, oldPath) => {
  if (newPath !== oldPath) {
    isMapMode.value = false
  }
  // 触发菜单变更事件
  const menuKey = route.name as string
  emit('menuChange', menuKey)

  // 控制搜索框显示
  const searchConfig: Record<string, boolean> = {
    'Yukoume': true,
    'FishingCircle': true,
    'FishingSpots': true,
    'AddSpot': false,
    'UserManagement': true,
    'Activities': true,
    'Profile': false,
    'CircleDetail': false  // 详情页面不需要搜索

  }

  const event = new CustomEvent('page-search-toggle', {
    detail: { showSearch: searchConfig[menuKey] || false },
    bubbles: true,
    cancelable: true
  })
  window.dispatchEvent(event)
})

// 监听地图模式切换事件
onMounted(() => {
  const handleToggleMapMode = (event: Event) => {
    const customEvent = event as CustomEvent
    isMapMode.value = customEvent.detail.isMapMode
    if (isMapMode.value) {
      mapController.activateMode(customEvent.detail.modeHandler,customEvent.detail.data)
    } else {
      mapController.deactivateCurrentMode()
    }
  }


  window.addEventListener('toggle-map-mode', handleToggleMapMode as EventListener)

  // 清理监听器
  return () => {
    window.removeEventListener('toggle-map-mode', handleToggleMapMode as EventListener)
  }
})

// 地图就绪回调
const onMapReady = (map: any) => {
  console.log('地图已就绪，可用于各功能页面')
}

// 切换菜单（通过路由）
const switchMenu = (menuKey: string) => {
  console.log('MainContainer: switchMenu 被调用', { menuKey, currentPath: route.path })

  // 映射菜单键到路由路径
  const menuRoutes: Record<string, string> = {
    'yukoume': '/yukoume',
    'fishingCircle': '/fishing-circle',
    'fishingSpots': '/fishing-spots',
    'activities': '/activities',
    'profile': '/profile',
    'addSpot': '/add-spot',
    'userManagement': '/user-management'
  }

  const targetPath = menuRoutes[menuKey]
  if (targetPath) {
    // 使用路由切换
    router.push(targetPath)
  }
}


// 退出地图模式
const exitMapMode = () => {
  console.log('退出地图模式')
  isMapMode.value = false
  mapController.deactivateCurrentMode()
}

// 生命周期钩子
onMounted(() => {
  initialize()
})

// 暴露方法给父组件
defineExpose({
  switchMenu,
  mapContainer,
  initialize
})
</script>

<style scoped>
.main-container {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  flex: 1;
}

.map-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  transition: z-index 0.3s ease;
}

.map-mode {
  z-index: 3;
}

.content-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  background: var(--bg-primary);
  backdrop-filter: blur(12px);
  transform: translateY(100%);
  transition: transform 0.3s ease, z-index 0.3s ease;
  overflow: hidden;
  /* 禁用滚动，让子组件处理滚动 */
  pointer-events: none;
  /* 默认禁止事件穿透 */
}

.content-layer.content-visible {
  pointer-events: auto;
  /* 可见时恢复事件 */
}

/* 地图模式时确保内容层在地图下方 */
.map-mode+.content-layer {
  z-index: 0;
  /* 确保在地图下方 */
}

.content-visible {
  transform: translateY(0);
}

.content-page {
  padding: 24px;
  max-width: 1200px;
  margin: 0 auto;
  min-height: 100vh;
}

.content-page h2 {
  margin-bottom: 16px;
  color: var(--text-primary);
  font-size: 24px;
}

.content-page p {
  color: var(--text-secondary);
  line-height: 1.6;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .content-page {
    padding: 16px;
  }
}

.content-placeholder {
  padding: 40px;
  text-align: center;
  color: var(--text-primary);
}

.content-placeholder h2 {
  margin-bottom: 16px;
  color: var(--text-primary);
  font-size: 24px;
}

.content-placeholder p {
  color: var(--text-secondary);
  font-size: 16px;
}
</style>